const validate = {
  error: '',
  // 金额
  isMoney(value, errorMsg = '金额信息错误') {
    const regMoney = /^(([1-9]\d*)|\d)(\.\d{1,2})?$/
    const result = regMoney.test(value)
    if (!result) { this.error = errorMsg; return false } else { return true }
  },

  // 密码
  isPassword(value, errorMsg = '密码需要至少6位字符，包含数字和字母2种类型字符，字母区分大小写') {
    const passwordReg = /^(?=.*[0-9])(?=.*[a-zA-Z])(.{6,}).*$/
    const result = passwordReg.test(value)
    if (!result) { this.error = errorMsg; return false } else { return true }
  },

  // 是否为空
  isNull(str, errorMsg = '必填信息不能为空') {
    if (str === 0) { return true }
    if (!str) { this.error = errorMsg; return false } else { return true }
  },

  // 手机号或者固话
  isLinkPhone(val, errorMsg = '手机号或者固话格式错误') {
    const telReg = /^0\d{2,3}-?\d{7,8}$/
    const phoneReg = /^1[3456789]\d{9}$/
    if (!telReg.test(val) && !phoneReg.test(val)) { this.error = errorMsg; return false } else { return true }
  },

  // 手机号
  isPhone(val, errorMsg = '手机号格式错误') {
    const phoneReg = /^1\d{10}$/
    if (!phoneReg.test(val)) { this.error = errorMsg; return false } else { return true }
  },

  // 判断是否为url地址
  isUrl(str, errorMsg = 'url地址格式错误') {
    const rex = /^(http|https){1}:\/\/[^\s]+$/
    if (!rex.test(str)) { this.error = errorMsg; return false } else { return true }
  },

  // 是否汉字
  isCN(str, errorMsg = '请输入汉字') {
    const reg = /^[\u4e00-\u9fa5]+$/
    if (!reg.test(str)) { this.error = errorMsg; return false } else { return true }
  },

  // 是否为邮箱
  isEmail(str, errorMsg = '邮箱格式错误') {
    const reg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/
    if (!reg.test(str)) { this.error = errorMsg; return false } else { return true }
  },

  // 是否为身份证
  isCard(str, errorMsg = '身份证格式错误') {
    const reg = /^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|30|31)|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}([0-9]|x|X)$/
    if (!reg.test(str)) { this.error = errorMsg; return false } else { return true }
  },

  isNum(num, errorMsg = '请输入数字') {
    const rex = /^[0-9]+$/
    if (!rex.test(num)) { this.error = errorMsg; return false } else { return true }
  },

  // 是否全为热线电话
  isHotline(num, errorMsg = '热线电话格式错误') {
    const rex = /^[0-9]([0-9]|-)*[0-9]$/
    if (rex.test(num)) { this.error = errorMsg; return false } else { return true }
  },

  // 判断数组长度
  isArrayLen(arr, errorMsg = '长度不能为空') {
    if (arr instanceof Array) {
      if (!arr.length) {
        this.error = errorMsg
        return false
      } else {
        return true
      }
    }
  },
  // 经度判断
  isLongitude(longitude, errorMsg = '经度格式错误') {
    const reg = /^(((\d|[1-9]\d|1[1-7]\d|0)\.\d{0,6})|(\d|[1-9]\d|1[1-7]\d|0{1,3})|180\.0{0,6}|180)$/
    if (!reg.test(longitude)) { this.error = errorMsg; return false } else { return true }
  },
  // 纬度判断
  isLatitude(latitude, errorMsg = '纬度格式错误') {
    const reg = /^([0-8]?\d\.\d{0,6}|90\.0{0,6}|[0-8]?\d|90)$/
    if (!reg.test(latitude)) { this.error = errorMsg; return false } else { return true }
  },
  // 判断端口号
  isPort(port, errorMsg = '端口号不正确') {
    const reg = /^([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{4}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/
    if (!reg.test(port)) { this.error = errorMsg; return false } else { return true }
  },
  comdify(n) {
    if (!n) return n
    let str = (n.toString()).split('.')
    let re = /\d{1,3}(?=(\d{3})+$)/g
    let n1 = str[0].replace(re, "$&,")
    return str.length > 1 && str[1] ? `${n1}.${str[1]}` : `${n1}`
  },
  comdifyZero(n) {
    if (!n) return n
    let str = (n.toString()).split('.')
    let re = /\d{1,3}(?=(\d{3})+$)/g
    let n1 = str[0].replace(re, "$&,")
    return str.length > 1 && str[1] ? `${n1}.${str[1]}` : `${n1}.00`
  },
  //去除千分位中的‘，’
  delcommafy(num) {
    if (!num) return num
    num = num.toString()
    num = num.replace(/,/gi, '')
    return Number(num)
  },
  //数字金额转换为汉字金额
  convertCurrency(money) {
    //汉字的数字
    let cnNums = new Array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖')
    //基本单位
    let cnIntRadice = new Array('', '拾', '佰', '仟')
    //对应整数部分扩展单位
    let cnIntUnits = new Array('', '万', '亿', '兆')
    //对应小数部分单位
    let cnDecUnits = new Array('角', '分', '毫', '厘')
    //整数金额时后面跟的字符
    let cnInteger = '整'
    //整型完以后的单位
    let cnIntLast = '元'
    //最大处理的数字
    let maxNum = 999999999999999.9999
    //金额整数部分
    let integerNum
    //金额小数部分
    let decimalNum
    //输出的中文金额字符串
    let chineseStr = ''
    //分离金额后用的数组，预定义
    let parts
    if (money == '') { return '' }
    money = parseFloat(money)
    if (money >= maxNum) {
      //超出最大处理数字
      return ''
    }
    if (money == 0) {
      chineseStr = cnNums[0] + cnIntLast + cnInteger
      return chineseStr
    }
    //转换为字符串
    money = money.toString()
    if (money.indexOf('.') == -1) {
      integerNum = money
      decimalNum = ''
    } else {
      parts = money.split('.')
      integerNum = parts[0]
      decimalNum = parts[1].substr(0, 4)
    }
    //获取整型部分转换
    if (parseInt(integerNum, 10) > 0) {
      let zeroCount = 0
      let IntLen = integerNum.length
      for (let i = 0; i < IntLen; i++) {
        let n = integerNum.substr(i, 1)
        let p = IntLen - i - 1
        let q = p / 4
        let m = p % 4
        if (n == '0') {
          zeroCount++
        } else {
          if (zeroCount > 0) {
            chineseStr += cnNums[0]
          }
          //归零
          zeroCount = 0
          chineseStr += cnNums[parseInt(n)] + cnIntRadice[m]
        }
        if (m == 0 && zeroCount < 4) {
          chineseStr += cnIntUnits[q]
        }
      }
      chineseStr += cnIntLast
    }
    //小数部分
    if (decimalNum != '') {
      let decLen = decimalNum.length
      for (let i = 0; i < decLen; i++) {
        let n = decimalNum.substr(i, 1)
        if (n != '0') {
          chineseStr += cnNums[Number(n)] + cnDecUnits[i]
        }
      }
    }
    if (chineseStr == '') {
      chineseStr += cnNums[0] + cnIntLast + cnInteger
    } else if (decimalNum == '') {
      chineseStr += cnInteger
    }
    return chineseStr
  },
  //四舍五入 val待处理的数，places保留的位数，type默认为数字 1为字符串
  keepDecimals(val, places, type) {
    if (val) {
      let num = val + ''
      let numStart = num.split('.')[0]
      let numEnd = num.split('.')[1]
      if (numEnd) {
        if (numEnd.length == (places + 1)) {
          num += '1'
          num = Number(num).toFixed(places)
        } else {
          let powVal = Math.pow(10, places)
          num = Math.round(num * powVal) / powVal + ''
          if (num.split('.')[1] && num.split('.')[1].length < places) {
            for (let n = 0; n < places - num.split('.')[1].length; n++) {
              num += '0'
            }
          }
        }
      } else {
        num = numStart + '.'
        for (let n = 0; n < places; n++) {
          num += '0'
        }
      }
      return type == 1 ? num = num + '' : num = num * 1
    } else {
      return val
    }
  }
}

export default validate